package com.czl.module_storehouse.activity.remand.details;

import android.annotation.SuppressLint;
import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.base.data.bean.tengyun.ReceiveBean;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.base.BaseDetailsActivity;
import com.czl.module_storehouse.adapter.RemandInfoAdapter;
import com.czl.module_storehouse.bean.RemandBean;
import com.czl.module_storehouse.databinding.HeaderRemandBorrowBinding;
import com.czl.module_storehouse.event.RemandEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author xubin
 */
public class RemandBorrowDetailsActivity extends BaseDetailsActivity {
    private static final String REMAND_BORROW = "borrow";
    private HeaderRemandBorrowBinding mHeaderBinding;
    private RemandInfoAdapter mAdapter;

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("借用详情");
        EventBus.getDefault().register(this);
    }

    @Override
    protected BaseQuickAdapter<?, ?> getAdapter() {
        if (mAdapter == null) {
            mAdapter = new RemandInfoAdapter(R.layout.item_remand_info, getType());
            mHeaderBinding = HeaderRemandBorrowBinding.inflate(getLayoutInflater());
            mAdapter.addHeaderView(mHeaderBinding.getRoot());
        }
        return mAdapter;
    }

    private int getType() {
        String type = getIntent().getStringExtra("type");
        if (REMAND_BORROW.equals(type)) {
            return Constants.Tag.TAG_BORROW;
        } else {
            return Constants.Tag.TAG_RECEIVE;
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(RemandEvent event) {
        if (event != null && event.getRemandBean() != null) {
            RemandBean remandBean = event.getRemandBean();
            ReceiveBean receiveBean = remandBean.getReceiveBean();
            BorrowBean borrowBean = remandBean.getBorrowBean();
            if (receiveBean != null) {
                mFooterBinding.etRemark.setText(receiveBean.getReturnComment());
                mAdapter.addData(receiveBean.getSortList());
            } else if (borrowBean != null) {
                mFooterBinding.etRemark.setText(borrowBean.getReturnComment());
                mAdapter.addData(borrowBean.getSortList());
            }
            initHeaderView(remandBean);
        }
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(RemandBean remandBean) {
        mHeaderBinding.clHeaderInfo.ivHeader.setImageResource(R.mipmap.icon_lingyong_head);
        if (remandBean == null || remandBean.getBorrowBean() == null) {
            return;
        }
        BorrowBean borrowBean = remandBean.getBorrowBean();
        mHeaderBinding.clHeaderInfo.tvHeaderTitle.setText(borrowBean.getBorrowName());
        mHeaderBinding.clHeaderInfo.tvHeaderDate.setText(borrowBean.getBorrowDate());
        mHeaderBinding.clHeaderInfo.tvHeaderCode.setText(borrowBean.getBorrowCode("借用单："));
        mHeaderBinding.tvHeaderCompany.setText("借用公司：" + borrowBean.getBorrowCompanyName());
        mHeaderBinding.tvHeaderDepartment.setText("借用部门：" + borrowBean.getBorrowPersonDepartment());
        mHeaderBinding.tvHeaderDesc.setText("借用原因：" + borrowBean.getApplyComment());
        mHeaderBinding.tvReturnDate.setText("拟还日期：" + borrowBean.getMoReturnDate());
        mHeaderBinding.tvHeaderActDate.setText("实还日期：" + borrowBean.getReturnDate());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
    }
}
